package com.xiaoluo.service;

import com.xiaoluo.cd.dao.CommitsMapper;
import com.xiaoluo.cd.entity.base.Commits;
import com.xiaoluo.cd.entity.base.CommitsExample;
import com.xiaoluo.web.form.GitCommit;
import com.xiaoluo.web.form.GitProject;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * Project : XL-CD
 * Package : com.xiaoluo.service
 * Created by Willian.
 * Date  : 2016/1/12 15:12.
 */
@Service
@Transactional
public class CommitService {

    @Autowired
    private CommitsMapper commitsMapper;

    public List<Commits> list(Long projId,Long commitId){
        CommitsExample example = new CommitsExample();
        CommitsExample.Criteria criteria = example.createCriteria();
        criteria.andProjIdEqualTo(projId);

        if(commitId != null){
            criteria.andIdEqualTo(commitId);
        }

        example.setOrderByClause("id desc");
        return  commitsMapper.selectByExample(example);
    }
    /**
     *
     * @param project
     * @param projId
     */
    public GitCommit save(GitProject project,Long projId){

        GitCommit result = null;
        for(GitCommit commit : project.getCommits()){
            try {
                Commits commits = new Commits();
                commits.setCreateAt(DateUtils.parseDate(commit.getTimestamp(), "yyyy-MM-dd'T'hh:mm:ss+08:00"));
                commits.setCommitName(commit.getId());
                commits.setMessage(commit.getMessage());
                commits.setUrl(commit.getUrl());
                commits.setUserName(commit.getAuthor().getName());
                commits.setEmail(commit.getAuthor().getEmail());
                commits.setProjId(projId);
                commits.setBranch(getBranch(project.getRef()));
                commits.setStatus((byte)0);
                result = commit;
                commitsMapper.insert(commits);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    private  String getBranch(String ref){
        if(ref != null && !"".equals(ref)){
            return ref.substring(ref.lastIndexOf("/")+1);
        }
        return "";
    }

    public Commits get(String name){
        CommitsExample example = new CommitsExample();
        example.createCriteria().andCommitNameEqualTo(name);
        List<Commits> list = commitsMapper.selectByExample(example);
        if(list != null && list.size()>0){
            return list.get(0);
        }

        return null;
    }

    public Commits findByProj(Long projId){
        CommitsExample example = new CommitsExample();
        example.createCriteria().andProjIdEqualTo(projId);

        example.setLimit(1);
        example.setOffset(0);
        example.setOrderByClause("id desc");

        List<Commits> list = commitsMapper.selectByExample(example);
        if(list != null && list.size()>0){
            return list.get(0);
        }

        return null;
    }

}
